import { ref } from 'vue'
import { defineStore } from 'pinia';
import tool from "@/utils";

export type UserInfo = {
    username: string,
    avatar: string,
    email?: string,
    ID: string,
    role_ID: string,
    menu: string[],
    rules: string[]
}

export const useUserInfoStore = defineStore('userInfo', () => {
    const userInfo = ref<UserInfo>({
        username: "",
        avatar: "",
        ID: "",
        role_ID: "",
        menu: [],
        rules: [],
        email: ""
    })

    /**获取用户信息*/
    const getUserInfo = ():UserInfo => {
        let userInfo = tool.getCookie('userInfo');
        let user: any = {}
        if (userInfo) {
            user = JSON.parse(decodeURIComponent(userInfo as string));
        }
        user.avatar = 'https://anran233.com/img/file-1677931608455.jpeg';
        setUserInfo(user)
        return user
    }

    /**设置userInfo*/
    const setUserInfo = (newUserInfo: UserInfo) => {
        userInfo.value = newUserInfo;
    }

    /**清空用户信息*/
    const clearUserInfo = () => {
        userInfo.value = {
            username: "",
            avatar: "",
            ID: "",
            role_ID: "",
            menu: [],
            rules: [],
            email: ""
        }
    }

    return { userInfo, setUserInfo, clearUserInfo, getUserInfo }
})